package BehaviorPattern.ChainOfResponsibility;

/**
 * @author: Themberfue
 * @date: 2025/8/13 11:26
 * @description:
 */
// 1. 请求类：报销单
class ExpenseRequest {
    private String purpose; // 报销用途
    private double amount;  // 报销金额

    public ExpenseRequest(String purpose, double amount) {
        this.purpose = purpose;
        this.amount = amount;
    }

    public String getPurpose() {
        return purpose;
    }

    public double getAmount() {
        return amount;
    }
}

// 2. 抽象处理者：审批者
abstract class Approver {
    protected Approver nextApprover; // 下一个审批者

    public Approver(Approver nextApprover) {
        this.nextApprover = nextApprover;
    }

    // 处理请求的抽象方法
    public abstract void processRequest(ExpenseRequest request);
}

// 3. 具体处理者：部门经理（审批小额费用）
class DepartmentManager extends Approver {
    public DepartmentManager(Approver nextApprover) {
        super(nextApprover);
    }

    @Override
    public void processRequest(ExpenseRequest request) {
        // 部门经理可以审批1000元以下的费用
        if (request.getAmount() <= 1000) {
            System.out.println("部门经理审批了 [" + request.getPurpose() + "] 的报销，金额: " + request.getAmount() + "元");
        }
        // 超过权限，交给下一个审批者
        else if (nextApprover != null) {
            nextApprover.processRequest(request);
        }
        // 如果没有下一个审批者，说明无人能处理
        else {
            System.out.println("[" + request.getPurpose() + "] 的报销金额过大，无人能审批");
        }
    }
}

// 3. 具体处理者：财务经理（审批中等金额）
class FinanceManager extends Approver {
    public FinanceManager(Approver nextApprover) {
        super(nextApprover);
    }

    @Override
    public void processRequest(ExpenseRequest request) {
        // 财务经理可以审批1000-5000元的费用
        if (request.getAmount() > 1000 && request.getAmount() <= 5000) {
            System.out.println("财务经理审批了 [" + request.getPurpose() + "] 的报销，金额: " + request.getAmount() + "元");
        }
        // 超过权限，交给下一个审批者
        else if (nextApprover != null) {
            nextApprover.processRequest(request);
        }
        else {
            System.out.println("[" + request.getPurpose() + "] 的报销金额过大，无人能审批");
        }
    }
}

// 3. 具体处理者：总经理（审批大额费用）
class GeneralManager extends Approver {
    public GeneralManager(Approver nextApprover) {
        super(nextApprover);
    }

    @Override
    public void processRequest(ExpenseRequest request) {
        // 总经理可以审批5000-10000元的费用
        if (request.getAmount() > 5000 && request.getAmount() <= 10000) {
            System.out.println("总经理审批了 [" + request.getPurpose() + "] 的报销，金额: " + request.getAmount() + "元");
        }
        // 超过权限，交给下一个审批者（如果有的话）
        else if (nextApprover != null) {
            nextApprover.processRequest(request);
        }
        else {
            System.out.println("[" + request.getPurpose() + "] 的报销金额过大，无人能审批");
        }
    }
}

// 4. 客户端代码
public class ChainOfResponsibilityExample {
    public static void main(String[] args) {
        // 构建责任链：部门经理 → 财务经理 → 总经理
        Approver generalManager = new GeneralManager(null); // 总经理是最后一个审批者
        Approver financeManager = new FinanceManager(generalManager);
        Approver departmentManager = new DepartmentManager(financeManager);

        // 创建不同金额的报销请求
        ExpenseRequest request1 = new ExpenseRequest("团队建设", 800);
        ExpenseRequest request2 = new ExpenseRequest("办公设备", 3500);
        ExpenseRequest request3 = new ExpenseRequest("客户招待", 8000);
        ExpenseRequest request4 = new ExpenseRequest("年度会议", 15000);

        // 提交报销请求（从责任链的第一个处理者开始）
        System.out.println("=== 提交报销请求 ===");
        departmentManager.processRequest(request1);
        departmentManager.processRequest(request2);
        departmentManager.processRequest(request3);
        departmentManager.processRequest(request4);
    }
}

